package com.ccp.dev.qbdms.consts;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ccp.dev.core.basic.api.ISysUser;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.PropertyUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.*;
import com.ccp.dev.extend.service.ExtendSysOrgService;
import com.ccp.dev.qbdms.annotation.ReportStatusName;
import com.ccp.dev.qbdms.enums.FlowStatusEnum;
import com.ccp.dev.qbdms.enums.GroupLevelStateCodeEnum;
import com.ccp.dev.qbdms.enums.HospitalLevelStateCodeEnum;
import com.ccp.dev.qbdms.enums.PlantLevelStateCodeEnum;
import com.ccp.dev.qbdms.model.*;
import com.ccp.dev.qbdms.service.SysOrgDeployService;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.ws.model.CodeZsj;
import com.ccp.dev.qbdms.ws.service.CodeZsjService;
import com.ccp.dev.system.consts.SecretUtil;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.DictionaryService;
import com.ccp.dev.system.service.SysOrgService;
import org.apache.commons.lang.StringEscapeUtils;
import org.flowable.bpmn.model.FlowElement;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.*;

@Component
public class ZhiLiangConst {

	/**
	 * 接口路径
	 */
	public static final String URL_API = "api/";
	/**
	 * 接口版本
	 */
	public static final String URL_VERSION = "v1/";

	/**
	 * api-url 上报
	 */

	public static final String API_URL_REPORT = "/report";

	/**
	 * api-url 上报
	 */
	public static final String API_URL_RECEIVE_ATTACHMENT = "receiveAttachment";
	/**
	 * api-url 无需上报
	 */
	public static final String API_URL_DONOT = "/donot";
	/**
	 * api-url 退回待修改
	 */
	public static final String API_URL_RETURNMOTIFY = "/rtnmotify";
	/**
	 * api-url 评定
	 */
	public static final String API_URL_EVALUATE = "/evaluate";
	/**
	 * api-url 督办下发
	 */
	public static final String API_URL_ISSUE = "/issue";
	/**
	 * 评定接口的模块
	 */
	public static final String EVALUATE_MODULE = "wQbdTrustPeople,wQbdTrustTeam";
	/**
	 * 根据所选单位，下发的模块
	 */
	public static final String ISSUE_MODULE = "wQbdFalSupervise";

	/**
	 * 上报接口中发送的参数名称
	 */
	public static final String API_REPORT_PARA_LIST = "list";
	/**
	 * 上报参数 - 附件参数
	 */
	public static final String API_REPORT_PARA_LIST_FILE = "fileList";
	/**
	 * 无需上报 退回待修改接口的参数名
	 */
	public static final String API_UPSTATUS_PARA_ID = "dataid";
	public static final String API_UPSTATUS_PARA_STATUS = "status";
	public static final String API_UPSTATUS_PARA_MSG = "returnMsg";
	public static final String API_UPSTATUS_PARA_TABLENAME = "tableName";

	/**
	 * 部署方式 - 集中部署
	 */
	public static final String DEPMETHOND_CENTER = "集中部署";
	/**
	 * 部署方式 - 分级部署
	 */
	public static final String DEPMETHOND_CLASS = "分级部署";
	/**
	 * 请求协议 - http
	 */
	public static final String protocol_http = "http";
	/**
	 * 请求协议 - https
	 */
	public static final String protocol_https = "https";

	/**
	 * 用于反射抓取service
	 */
	public static final String SERVICE = "Service";
	/**
	 * 上报使用 - 反射调用getone的方法名
	 */
	public static final String REPGETONE = "getParentAndChilden";
	/**
	 * 保存时反射调用保存方法
	 */
	public static final String SAVEALL = "saveAll";
	/**
	 * 更新时反射调用更新方法
	 */
	public static final String UPDATE = "update";
	/**
	 * 批量更新时反射调用批量更新方法
	 */
	public static final String UPDATEBATCHBYID = "updateBatchById";

	/**
	 * 2020-6-9 zzy 发动机试车模块 - 数据来源6 表示数据来自于六院
	 */
	public static final String FDJSC_DOF_6 = "6y";

	/**
	 * 查询过滤状态
	 */
	public static final String QUERYFILTERSTATUS = "0";

	/**
	 * 三级单位内部审批所需状态
	 */
	//待审批
	@ReportStatusName(value = "待审批", SystemLevel = BaseUtil.SystemLevel.PLANTLEVEL, isGlobeShow = false)
	public static final String INTERNALAPPROVAL_THEAPPROVALISPENDING = "1";
	//审批中
	@ReportStatusName(value = "审批中", SystemLevel = BaseUtil.SystemLevel.PLANTLEVEL, isGlobeShow = false)
	public static final String INTERNALAPPROVAL_INEXAMINATIONANDAPPROVAL = "2";
	//审批未通过
	@ReportStatusName(value = "审批未通过", SystemLevel = BaseUtil.SystemLevel.PLANTLEVEL, isGlobeShow = false)
	public static final String INTERNALAPPROVAL_APPROVALFAILED = "3";

	/**
	 * 三级单位
	 */
	//待上报
	@ReportStatusName(value = "待上报", SystemLevel = BaseUtil.SystemLevel.PLANTLEVEL, isRepeatAllowEdit = true, isSingleAllowEdit = true, isAllowDel = true)
	public static final String REPORT_STATUS_TEMP_TOBEREPORTED = "4";
	//报院审批中
	@ReportStatusName(value = "报院审批中", SystemLevel = BaseUtil.SystemLevel.PLANTLEVEL)
	public static final String REPORT_STATUS_TEMP_REPORTTOTHECOURTFORAPPROVAL = "5";
	//报院审批未通过
	@ReportStatusName(value = "报院审批未通过", SystemLevel = BaseUtil.SystemLevel.PLANTLEVEL, isRepeatAllowEdit = true, isSingleAllowEdit = true)
	public static final String REPORT_STATUS_TEMP_FAILEDPASSAPPROVALOFTHEHOSPITAL = "6";
	//报院中
	@ReportStatusName(value = "报院中", SystemLevel = BaseUtil.SystemLevel.PLANTLEVEL)
	public static final String REPORT_STATUS_TEMP_REPORTTOTHEHOSPITAL = "7";
	//报院成功
	@ReportStatusName(value = "报院成功", SystemLevel = BaseUtil.SystemLevel.PLANTLEVEL, isRepeatAllowEdit = true)
	public static final String REPORT_STATUS_TEMP_REPORTTOTHEHOSPITAL_SUCCESS = "8";
	//报院失败
	@ReportStatusName(value = "报院失败", SystemLevel = BaseUtil.SystemLevel.PLANTLEVEL, isRepeatAllowEdit = true, isSingleAllowEdit = true)
	public static final String REPORT_STATUS_TEMP_REPORTTOTHEHOSPITAL_FAILURE = "9";
	//退回待修改
	@ReportStatusName(value = "退回待修改", SystemLevel = BaseUtil.SystemLevel.PLANTLEVEL, isRepeatAllowEdit = true, isSingleAllowEdit = true)
	public static final String PLACE_RETURN_UPDATE = "10";
	//无需上报,院退回,所级显示
	@ReportStatusName(value = "无需上报", SystemLevel = BaseUtil.SystemLevel.PLANTLEVEL)
	public static final String NOESCALATIONREQUIRED = "11";

	/**
	 * 二级单位
	 */
	//待报集团
	@ReportStatusName(value = "待报集团", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL, isRepeatAllowEdit = true, isSingleAllowEdit = true, isAllowDel = true)
	public static final String REPORT_STATUS_GROUPTOBEREPORTED = "12";
	//报集团审批中
	@ReportStatusName(value = "报集团审批中", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL)
	public static final String REPORT_STATUS_SUBMITTEDTOTHEGROUPFORAPPROVAL = "13";
	//报集团审批未通过
	@ReportStatusName(value = "报集团审批未通过", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL, isRepeatAllowEdit = true, isSingleAllowEdit = true)
	public static final String REPORT_STATUS_FAILEDTOSUBMITTOTHEGROUPFORAPPROVAL = "14";
	//报集团中
	@ReportStatusName(value = "报集团中", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL)
	public static final String REPORT_STATUS_REPORTEDTOTHEGROUP = "15";
	//报集团成功
	@ReportStatusName(value = "报集团成功", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL, isRepeatAllowEdit = true)
	public static final String REPORT_STATUS_REPORTGROUPSUCCESS = "16";
	//报集团失败
	@ReportStatusName(value = "报集团失败", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL, isRepeatAllowEdit = true, isSingleAllowEdit = true)
	public static final String REPORT_STATUS_REPORTGROUPFAILURE = "17";
	//集团退回
	@ReportStatusName(value = "集团退回", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL, isRepeatAllowEdit = true, isSingleAllowEdit = true)
	public static final String GROUP_RETURN = "18";
	//院退回中
	@ReportStatusName(value = "院退回中", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL)
	public static final String HOSPITAL_RETURNING = "19";
	//院退回失败
	@ReportStatusName(value = "院退回失败", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL, isRepeatAllowEdit = true)
	public static final String HOSPITAL_FAILEDTORETURN = "20";
	/**
	 * 无需上报,集团退回,院级显示
	 */
	@ReportStatusName(value = "无需上报", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL)
	public static final String HOSPITAL_DONOT_REPORT = "31";


	/**
	 * 一级单位
	 */
	//待确认
	@ReportStatusName(value = "待确认", SystemLevel = BaseUtil.SystemLevel.GROUPLEVEL, isRepeatAllowEdit = true)
	public static final String GROUP_TOBECONFIRMED = "21";
	//已确认
	@ReportStatusName(value = "已确认", SystemLevel = BaseUtil.SystemLevel.GROUPLEVEL, isRepeatAllowEdit = true)
	public static final String GROUP_CONFIRMED = "22";
	//集团退回中
	@ReportStatusName(value = "集团退回中", SystemLevel = BaseUtil.SystemLevel.GROUPLEVEL)
	public static final String GROUP_RETURNING = "23";
	//集团退回失败
	@ReportStatusName(value = "集团退回失败", SystemLevel = BaseUtil.SystemLevel.GROUPLEVEL, isRepeatAllowEdit = true)
	public static final String GROUP_RETURNFAILED = "24";

	/**
	 * 二级单位内部审核流程所需状态
	 */
	//院内待审批
	@ReportStatusName(value = "院内待审批", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL, isGlobeShow = false, isSingleAllowEdit = true)
	public static final String HOSPITAL_INTERNALAPPROVAL_THEAPPROVALISPENDING = "25";
	//院内审批中
	@ReportStatusName(value = "院内审批中", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL, isGlobeShow = false)
	public static final String HOSPITAL_INTERNALAPPROVAL_INEXAMINATIONANDAPPROVAL = "26";
	//院内审批未通过
	@ReportStatusName(value = "院内审批未通过", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL, isGlobeShow = false, isSingleAllowEdit = true)
	public static final String HOSPITAL_INTERNALAPPROVAL_APPROVALFAILED = "27";

	@ReportStatusName(value = "集团新增", SystemLevel = BaseUtil.SystemLevel.GROUPLEVEL, isGlobeShow = false, isSingleAllowEdit = true, isAllowDel = true)
	public static final String GROUP_ADD = "30";


	// **********************   无需上报所需状态    **********************
	@ReportStatusName(value = "集团无需上报失败", SystemLevel = BaseUtil.SystemLevel.GROUPLEVEL)
	public static final String GROUP_DONOT_NEED_REPORT_FAILURE = "28";

	@ReportStatusName(value = "院无需上报失败", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL)
	public static final String HOSPITAL_DONOT_NEED_REPORT_FAILURE = "29";

	@ReportStatusName(value = "集团无需上报中", SystemLevel = BaseUtil.SystemLevel.GROUPLEVEL)
	public static final String GROUP_DONOT_REPORT_ING = "32";

	@ReportStatusName(value = "院无需上报中", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL)
	public static final String HOSPITAL_DONOT_REPORT_ING = "33";

	// **********************   退回待修改所需状态码    **********************

	/**
	 * 集团退回待修改中
	 */
	@ReportStatusName(value = "退回待修改中", SystemLevel = BaseUtil.SystemLevel.GROUPLEVEL)
	public static final String GROUP_RETURN_UPDATE_ING = "34";
	/**
	 * 院退回待修改中
	 */
	@ReportStatusName(value = "退回待修改中", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL)
	public static final String HOSPITAL_RETURN_UPDATE_ING = "35";
	/**
	 * 集团退回待修改失败
	 */
	@ReportStatusName(value = "退回待修改失败", SystemLevel = BaseUtil.SystemLevel.GROUPLEVEL)
	public static final String GROUP_RETURN_UPDATE_FAIL = "36";
	/**
	 * 院退回待修改失败
	 */
	@ReportStatusName(value = "退回待修改失败", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL)
	public static final String HOSPITAL_RETURN_UPDATE_FAIL = "37";

	/**
	 * 集团显示退回待修改
	 */
	@ReportStatusName(value = "退回待修改", SystemLevel = BaseUtil.SystemLevel.GROUPLEVEL)
	public static final String GROUP_RETURN_UPDATE = "38";
	/**
	 * 院级显示退回待修改
	 */
	@ReportStatusName(value = "退回待修改", SystemLevel = BaseUtil.SystemLevel.HOSPITALLEVEL)
	public static final String HOSPITAL_RETURN_UPDATE = "39";

	//-- 飞行试验奖惩计划 下发状态 开始 --------------------------------------------------------------------------------------
	//-- 集团级质量综合分析报告 使用此下发状态 20200623
	/**
	 * 待下发
	 */
	public static final String GROUP_PREPARE_SEND = "1";
	/**
	 * 下发中
	 */
	public static final String GROUP_LOADING_SEND = "2";
	/**
	 * 下发失败
	 */
	public static final String GROUP_FAILURE_SEND2 = "3";
	/**
	 * 已下发
	 */
	public static final String GROUP_ALREADY_SEND3 = "4";
	/**
	 * 已审减
	 */
	public static final String GROUP_REVIEW = "5";

	/**
	 * 不定义枚举了，封装一个map，用于获取状态text
	 */
	public static final Map<String, String> HAIR_DOWN_MAP;

	static {
		Map map = new HashMap();
		map.put(GROUP_PREPARE_SEND, "待下发");
		map.put(GROUP_LOADING_SEND, "下发中");
		map.put(GROUP_FAILURE_SEND2, "下发失败");
		map.put(GROUP_ALREADY_SEND3, "已下发");
		map.put(GROUP_REVIEW, "已审减");
		HAIR_DOWN_MAP = Collections.unmodifiableMap(map);

	}
	//-- 飞行试验奖惩计划 下发状态 结束 --------------------------------------------------------------------------------------

	//质量奖惩信息状态 开始---------------------------------------------------------------------------------------------------
	//责任追究
	public static final String RESPONSIBILITY_INVESTIGATION = "责任追究";
	//奖励
	public static final String REWARD = "奖励";
	//处罚
	public static final String PUNISH = "处罚";
	//质量问题
	public static final String QUALITY_PROBLEM = "质量问题";
	//飞行试验
	public static final String FLIGHT_TEST = "飞行试验";
	//其他
	public static final String RESTS = "其他";
	//质量奖惩信息状态 结束---------------------------------------------------------------------------------------------------

	//页面编辑时默认缺省密级为70秘密
	public static final Integer DEFAULT_SECRET = 70;

	/**
	 * 判断该业务数据是否允许编辑 根据系统等级和状态位，判断是否允许编辑，将结果放入每个对象，并返回该判断结果结果
	 *
	 * @param isRepeatReport 是否重复上报
	 */
	public static boolean isDataAllowEdit(BaseBusinessModel baseModel, boolean isRepeatReport) {
		BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
		Class clazz = ZhiLiangConst.class;
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			ReportStatusName annotation = field.getAnnotation(ReportStatusName.class);
			if (Objects.isNull(annotation)) {
				continue;
			}

			if (annotation.SystemLevel() != currentSystemLevel) {
				continue;
			}

			try {
				String status = (String) field.get(clazz);
				//匹配到该业务数据的状态位注解
				if (status.equals(baseModel.getReportStatus())) {
					boolean allowEdit = isRepeatReport ? annotation.isRepeatAllowEdit()
							: annotation.isSingleAllowEdit();
					baseModel.setAllowEdit(allowEdit);
					break;
				}
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return baseModel.isAllowEdit();
	}


	public static void isDataAllowEditByFlowStatus(List baseBusinessModelList) {
		for (int i = 0; i < baseBusinessModelList.size(); i++) {
			BaseBusinessModel baseBusinessModel = (BaseBusinessModel) baseBusinessModelList.get(i);
			FlowStatusEnum flowStatusEnum = FlowStatusEnum
					.getEnumByValue(baseBusinessModel.getFlowStatus());
			if (null != flowStatusEnum) {
				baseBusinessModel.setAllowEdit(flowStatusEnum.isEdit());
				baseBusinessModel.setFlowStatusStr(flowStatusEnum.getName());
				baseBusinessModel.setAllowDel(flowStatusEnum.isDel());
			}
		}
	}

	/**
	 * 审核不符合项
	 */
	public static void isDataAllowEditByFlowStatusSHBFHX(List baseBusinessModelList) {
		for (int i = 0; i < baseBusinessModelList.size(); i++) {
			BaseBusinessModel baseBusinessModel = (BaseBusinessModel) baseBusinessModelList.get(i);
			FlowStatusEnum flowStatusEnum = FlowStatusEnum
					.getEnumByValue(baseBusinessModel.getFlowStatus());
			if (null != flowStatusEnum) {
				baseBusinessModel.setAllowEdit(flowStatusEnum.isEdit());
				baseBusinessModel.setFlowStatusStr(flowStatusEnum.getName());
				baseBusinessModel.setAllowDel(flowStatusEnum.isDel());
			}
		}
	}

	/**
	 * 判断该业务数据是否允许编辑
	 *
	 * @param baseModelList 需要确定是否能编辑的list
	 * @param isRepeatReport 是否重复上报标识
	 */
	public static boolean isDataAllowEdit(List baseModelList, boolean isRepeatReport) {
		BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
		Class clazz = ZhiLiangConst.class;
		Field[] fields = clazz.getDeclaredFields();
		boolean result = true;
		for (int i = 0; i < baseModelList.size(); i++) {
			BaseBusinessModel baseModel = (BaseBusinessModel) baseModelList.get(i);
			for (Field field : fields) {
				ReportStatusName annotation = field.getAnnotation(ReportStatusName.class);
				if (Objects.isNull(annotation)) {
					continue;
				}

				if (annotation.SystemLevel() != currentSystemLevel) {
					continue;
				}

				try {
					String status = (String) field.get(clazz);
					//匹配到该业务数据的状态位注解
					if (status.equals(baseModel.getReportStatus())) {
						boolean allowEdit =
								isRepeatReport ? annotation.isRepeatAllowEdit() : annotation.isSingleAllowEdit();
						baseModel.setAllowEdit(allowEdit);
						baseModel.setAllowDel(annotation.isAllowDel());

						if (!baseModel.isAllowEdit()) {
							result = false;
						}
						break;
					}
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}


	/**
	 * 得到状态的数据字典格式，方便做下拉，得到isGlobeShow不为false的
	 */
	public static List<Dictionary> getStatusDic() throws IllegalAccessException {
		BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
		Class clazz = ZhiLiangConst.class;
		Field[] fields = clazz.getDeclaredFields();
		List<Dictionary> dicList = new ArrayList<>();
		for (Field field : fields) {
			ReportStatusName annotation = field.getAnnotation(ReportStatusName.class);
			if (Objects.nonNull(annotation)) {
				if (annotation.SystemLevel() == currentSystemLevel && annotation.isGlobeShow()) {
					Dictionary dictionary = new Dictionary();
					dictionary.setItemName(annotation.value());
					dictionary.setItemValue((String) field.get(clazz));
					dicList.add(dictionary);
				}
			}
		}
		return dicList;
	}

	/**
	 * 得到状态的数据字典格式，方便做下拉，得到所有状态
	 */
	public static List<Dictionary> getStatusDicAll() throws IllegalAccessException {
		BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
		Class clazz = ZhiLiangConst.class;
		Field[] fields = clazz.getDeclaredFields();
		List<Dictionary> dicList = new ArrayList<>();
		for (Field field : fields) {
			ReportStatusName annotation = field.getAnnotation(ReportStatusName.class);
			if (Objects.nonNull(annotation)) {
				if (annotation.SystemLevel() == currentSystemLevel) {
					Dictionary dictionary = new Dictionary();
					dictionary.setItemName(annotation.value());
					dictionary.setItemValue((String) field.get(clazz));
					dicList.add(dictionary);
				}
			}
		}
		return dicList;
	}

	/**
	 * 通过状态位得到状态名，方便列表显示
	 */
	public static String getStatusStrByInt(String reportStatus) throws IllegalAccessException {
		if (Objects.isNull(reportStatus)) {
			return "";
		}
		Class clazz = ZhiLiangConst.class;
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			ReportStatusName annotation = field.getAnnotation(ReportStatusName.class);
			field.get(clazz).getClass().getName();
			Object o1 = field.get(clazz);
			if (o1 instanceof java.util.Map) {
				continue;
			}
			if (reportStatus.equals((String) field.get(clazz))) {
				return annotation.value();
			}
		}
		return "待上报";
	}


	/**
	 * 上报流程审批中所勾选通过数据id
	 */
	public static final String SUCCESS_DATA_ID = "successDataId";

	/**
	 * 如果有三级单位内部审批流程，那么初始化数据状态是待审批 如果没有，那么初始化数据状态是待上报
	 */
	public static String getDataInitStatusToHospital() {
		boolean needInnerApprove = PropertyUtil.getBooleanByAlias("needInnerApprove");

		if (BaseUtil.getCurrentSystemLevel() == BaseUtil.SystemLevel.GROUPLEVEL) {
			return GROUP_ADD;
		} else if (BaseUtil.getCurrentSystemLevel() == BaseUtil.SystemLevel.HOSPITALLEVEL) {
			return REPORT_STATUS_GROUPTOBEREPORTED;
		} else {
			if (needInnerApprove) {
				return INTERNALAPPROVAL_THEAPPROVALISPENDING;
			} else {
				return REPORT_STATUS_TEMP_TOBEREPORTED;
			}
		}
	}

	/**
	 * 报集团的初始化状态位
	 */
	public static String getDataInitStatusToGroup() {
		return REPORT_STATUS_GROUPTOBEREPORTED;
	}

	/**
	 * 附带检索条件的分页查询添加密级过滤和数据隔离 //2020/7/29 zzy  增加上报状态过滤
	 *
	 * @author 创新中心-王明亮
	 */
	public static void addQueryFilter(QueryFilter queryFilter) {
		SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
		// 过滤密级
		queryFilter.addFilter("secret", SecretUtil.getUserDataSecretMap().get(sysUser.getPrivacyLevel()) + "");
        
		// 集团看所有数据,院级看本单位所有部厂所的数据,各个三级单位只看本单位的数据
		queryFilter.addFilter("belongdeptname", BaseUtil.getCurrentTenOrgCodePath() + "%");
		//2020/7/29 zzy  增加上报状态过滤
		addFlowStatus(queryFilter);
//        addReportCodeConditions(queryFilter);
	}

	protected static void addFlowStatus(QueryFilter queryFilter) {
		if (null != queryFilter.get("flowStatus")) {
			queryFilter.addFilter("flowStatus",
					FlowStatusEnum.getFlowStatusQueryFilter(queryFilter.get("flowStatus").toString()));
		}

	}

	/**
	 * 附带检索条件的分页查询添加密级过滤和数据隔离
	 *
	 * @author 创新中心-王明亮
	 */
	public static void addQueryFilterNoReportStatus(QueryFilter queryFilter) {
		SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
		// 过滤密级
		queryFilter.addFilter("secret", SecretUtil.getUserDataSecretMap().get(sysUser.getPrivacyLevel()) + "");
        // 集团看所有数据,院级看本单位所有部厂所的数据,各个三级单位只看本单位的数据
		queryFilter.addFilter("belongdeptname", BaseUtil.getCurrentTenOrgCodePath() + "%");
	}

	/**
	 * 显示中文名而不是id、code等
	 *
	 * @param baseModelList 需要显示名字的的list
	 * @author 创新中心-王明亮
	 */
	public static void showName(List baseModelList) throws IllegalAccessException {
		DictionaryService dictionaryService = AppUtil.getBean(DictionaryService.class);
		SysOrgService sysOrgService = AppUtil.getBean(SysOrgService.class);
		// 密级数据字典
        CodeZsjService codeZsjService = AppUtil.getBean(CodeZsjService.class);
        List<CodeZsj> zsjList = codeZsjService.getByCodeName("信息涉密等级",null);
		for (Object o : baseModelList) {
			BaseBusinessModel baseModel = (BaseBusinessModel) o;
			boolean isNodeKeyExists = false;
			for (CodeZsj codeZsj : zsjList) {
				if (codeZsj.getCode().equals(baseModel.getSecret())) {
					isNodeKeyExists = true;
					// 密级显示名称
					baseModel.setSecret(codeZsj.getName());
				}
			}
			if (!isNodeKeyExists) {
				baseModel.setSecret("");
			}
			if (StringUtil.isNotEmpty(baseModel.getReportStatus())) {
				// 上报状态显示名称
				//baseModel.setReportStatusStr(ZhiLiangConst.getStatusStrByInt(baseModel.getReportStatus()));

				//2020/7/29 zzy 修改上报状态文字填充
				converReportCode2Name(baseModel);
			}
			// 台账列表中二三级单位显示的数据来源单位
			if (StringUtil.isNotEmpty(baseModel.getFilldeptid())) {
				baseModel
						.setFilldept(sysOrgService.getSysOrgByOrgCode(baseModel.getFilldeptid()).getOrgName());
			}
			// 台账列表中集团显示的数据来源单位
			if (StringUtil.isNotEmpty(baseModel.getFilldeptHospitalId())) {
				baseModel.setFilldeptHospital(
						sysOrgService.getSysOrgByOrgCode(baseModel.getFilldeptHospitalId()).getOrgName());
			}
		}
	}

	/**
	 * 公用字段赋值
	 */
	public static void addCommonFields(Object model) {
		if (model instanceof Collection) {
			((List<?>) model).forEach(a -> {
				if (a instanceof BaseBusinessModel) {
					addCommonFields((BaseBusinessModel<?>) a);
				}
			});
		} else if (model instanceof BaseBusinessModel) {
			addCommonFields((BaseBusinessModel<?>) model);
		}
	}

	/**
	 * 公用字段赋值
	 */
	public static void updateCommonFields(Object model) {
		if (model instanceof Collection) {
			((List<?>) model).forEach(a -> {
				if (a instanceof BaseBusinessModel) {
					updateCommonFields((BaseBusinessModel<?>) a);
				}
			});
		} else if (model instanceof BaseBusinessModel) {
			updateCommonFields((BaseBusinessModel<?>) model);
		}
	}

	/**
	 * 保存公用字段
	 *
	 * @param baseModel 实体
	 * @description 保存公用字段。
	 */
	private static void addCommonFields(BaseBusinessModel<?> baseModel) {
		ISysUser currentUser = ContextUtil.getCurrentUser();
		String userId = currentUser.getUserId();
		ExtendSysOrgService extendSysOrgService = AppUtil.getBean(ExtendSysOrgService.class);
		SysOrg currentTenOrg = extendSysOrgService.getTenOrg(userId);

		baseModel.setTenantId(ContextUtil.getCurrentUserTenantId());
		baseModel.setCreateBy(userId);
		baseModel.setCreateTime(new Date());
		baseModel.setFilldept(currentTenOrg.getOrgName());
		baseModel.setFilldeptid(currentTenOrg.getCode());
		baseModel.setFillman(currentUser.getFullName());
		baseModel.setFillmanaccount(currentUser.getAccount());
		baseModel.setFilldate(DateFormatUtil.format(new Date(), StringPool.DATE_FORMAT_DATE));
		baseModel.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath());
	}

	/**
	 * 更新基础公共字段
	 *
	 * @param baseModel model
	 */
	private static void updateCommonFields(BaseBusinessModel<?> baseModel) {
		ISysUser currentUser = ContextUtil.getCurrentUser();
		String userId = currentUser.getUserId();
		ExtendSysOrgService extendSysOrgService = AppUtil.getBean(ExtendSysOrgService.class);
		SysOrg currentTenOrg = extendSysOrgService.getTenOrg(userId);
		baseModel.setUpdateBy(userId);
		baseModel.setUpdateTime(new Date());
		baseModel.setFilldept(currentTenOrg.getOrgName());
		baseModel.setFillman(currentUser.getFullName());
		baseModel.setFilldate(DateFormatUtil.format(new Date(), StringPool.DATE_FORMAT_DATE));
	}

	/**
	 * 导出的时候增加公共的导出条件
	 *
	 * @param queryFilter 查询过滤条件
	 * @param ids 是否是选择性导出
	 * @return jsonArray 用于导出
	 */
	public static JSONArray exportAddConditions(QueryFilter queryFilter, String[] ids) {
		if (BeanUtils.isNotEmpty(ids)) {
			queryFilter.put("byId", true);
			queryFilter.put("id", ids);
		} else {
			queryFilter.put("byId", false);
			//增加不需要查询的状态，退回待修改，无需上报的数据
			queryFilter.put("noQueryStatus", ZhiLiangConst.QUERYFILTERSTATUS);
		}
		addQueryFilter(queryFilter);
		String htmStr = StringEscapeUtils.unescapeHtml(String.valueOf(queryFilter.get("data")));
		JSONObject allJson = JSONObject.parseObject(htmStr);
		String secret = allJson.getString("secret");
		queryFilter.addFilter("secret", secret);
		//2020/7/29 zzy  增加上报状态过滤
//        addReportCodeConditions(queryFilter);
		return allJson.getJSONArray("dataArray");
	}

	/**
	 * 获取当前登录人的密级,返回密级code对应的文字 !!! 注意,这里的密级是 人员密级 !!!
	 *
	 * @auth zzy
	 */
	public static String getCurrentUserSecretStr() {
		DictionaryService dictionaryService = AppUtil.getBean(DictionaryService.class);
		SysOrgService sysOrgService = AppUtil.getBean(SysOrgService.class);
		SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
		// 过滤密级
		List<Dictionary> dicList = dictionaryService.getDicByNodeKey("rymj", false);
		for (Dictionary dictionary : dicList) {
			if (dictionary.getItemValue().equals(sysUser.getPrivacyLevel().toString())) {
				// 密级显示名称
				return dictionary.getItemName();
			}
		}
		return "";
	}

	/**
	 * 添加上报状态码到检索条件
	 *
	 * @auth zzy
	 */
	public static void addReportCodeConditions(Map queryFilter) {
		StringBuilder sb = new StringBuilder();
		List<String> codelist = CollectionUtil.newList();
		switch (BaseUtil.getCurrentSystemLevel()) {
			case GROUPLEVEL:
				codelist = GroupLevelStateCodeEnum.getAllCode();
				break;
			case HOSPITALLEVEL:
				codelist = HospitalLevelStateCodeEnum.getAllCode();
				break;
			case PLANTLEVEL:
				codelist = PlantLevelStateCodeEnum.getAllCode();
				break;
		}
		for (String code : codelist) {
			sb.append("'" + code + "',");
		}
		if (sb.length() > 0) {
			queryFilter.put("reportStatus", sb.toString().substring(0, sb.length() - 1));
		}
	}

	/**
	 * 根据状态码来填充状态名称 //2020/8/14 zzy 增加逆推状态码,用于下级单位的列表显示以及编辑删除控制.
	 *
	 * @auth zzy
	 */
	private static void converReportCode2Name(BaseBusinessModel model) {
		switch (BaseUtil.getCurrentSystemLevel()) {
			case GROUPLEVEL:
				model.setReportStatusStr(GroupLevelStateCodeEnum.getNameByCode(model.getReportStatus()));
				model
						.setReportStatus(GroupLevelStateCodeEnum.getCodeByRelatedCode(model.getReportStatus()));
				break;
			case HOSPITALLEVEL:
				model.setReportStatusStr(HospitalLevelStateCodeEnum.getNameByCode(model.getReportStatus()));
				model.setReportStatus(
						HospitalLevelStateCodeEnum.getCodeByRelatedCode(model.getReportStatus()));
				break;
			case PLANTLEVEL:
				model.setReportStatusStr(PlantLevelStateCodeEnum.getNameByCode(model.getReportStatus()));
				model
						.setReportStatus(PlantLevelStateCodeEnum.getCodeByRelatedCode(model.getReportStatus()));
				break;
		}
	}


	/**
	 * 通过code获取SysOrgDeploy对象,对应的code不存在则返回Null
	 *
	 * @param code 组织code
	 * @author zzy
	 */
	public static SysOrgDeploy getOrgDeploymentByCode(String code) {
		SysOrgDeployService sysorgdep = AppUtil.getBean(SysOrgDeployService.class);
		SysOrgDeploy dep = sysorgdep.getDeployMethodByCode(code);
		return dep;
	}

	/**
	 * 指定的组织code是否是集中部署,如果没有对应的数据则默认认为是集中部署
	 *
	 * @param code 组织code
	 * @return 是否集中部署
	 * @author zzy
	 */
	public static boolean isCentralizedDeployment(String code) {
		SysOrgDeploy dep = getOrgDeploymentByCode(code);
		if (BeanUtils.isEmpty(dep) || DEPMETHOND_CENTER.equals(dep.getDepmethod())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断当前单位是否集中部署,如果没有配置上级单位的部署方式则为集中部署
	 *
	 * @return true 集中部署 false 分级部署
	 */
	public static boolean isCentralizedDeployment() {
		SysOrgDeployService sysorgdep = AppUtil.getBean(SysOrgDeployService.class);
		SysOrgDeploy dep = sysorgdep.getDeployMethod();
		if (BeanUtils.isEmpty(dep) || DEPMETHOND_CENTER.equals(dep.getDepmethod())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据组织code获取对应的服务地址,如果是集中部署或者没有对应数据则返回空串
	 *
	 * @param code 组织code
	 * @return 如果是集中部署返回空串, 分级部署则返回url
	 * @author zzy
	 */
	public static String getOrgUrlByCode(String code) {
		SysOrgDeploy dep = getOrgDeploymentByCode(code);
		if (BeanUtils.isEmpty(dep) || DEPMETHOND_CENTER.equals(dep.getDepmethod())) {
			return "";
		}
		return dep.getUrl();
	}

	/**
	 * 根据传入的状态code获取对应的关联code,用于构建sql中的 in('xx','xx')条件 2020-8-24 目前只在质量问题的搜索条件-状态 中使用.
	 *
	 * @author
	 */
	public static String wrapStatusCode(String status) {
		if (StringUtil.isEmpty(status)) {
			return "";
		}
		List<String> rel = new ArrayList<>();
		if (BaseUtil.getCurrentSystemLevel() == BaseUtil.SystemLevel.GROUPLEVEL) {
			rel = GroupLevelStateCodeEnum.getRelatedCodeByCode(status);
		} else if (BaseUtil.getCurrentSystemLevel() == BaseUtil.SystemLevel.HOSPITALLEVEL) {
			rel = HospitalLevelStateCodeEnum.getRelatedCodeByCode(status);
		} else if (BaseUtil.getCurrentSystemLevel() == BaseUtil.SystemLevel.PLANTLEVEL) {
			rel = PlantLevelStateCodeEnum.getRelatedCodeByCode(status);
		}

		if (BeanUtils.isEmpty(rel)) {
			return StringPool.SINGLE_QUOTE + status + StringPool.SINGLE_QUOTE;
		}
		rel.add(status);
		StringBuilder sb = new StringBuilder();
		for (String code : rel) {
			sb.append("'" + code + "',");
		}
		if (sb.length() > 0) {
			return sb.toString().substring(0, sb.length() - 1);
		}
		return "";
	}

	/**
	 * 剔除不是该部门的数据
	 */
	public static void showWQbdQualitymanageSu(List<WQbdQualitymanageSu> wQbdQualitymanageSus,
			String code) {

		if (BeanUtils.isNotEmpty(wQbdQualitymanageSus)) {
			Iterator<WQbdQualitymanageSu> iterator = wQbdQualitymanageSus.iterator();
			while (iterator.hasNext()) {
				WQbdQualitymanageSu next = iterator.next();
				if (!next.getResponsibleunit().equals(code)) {
					iterator.remove();
				}
			}
		}

	}
	/**
	 *筛选负责人数据
	 */
	public static  void  showWQbdQualityRevSu(List<WQbdQualityRevSu> wQbdQualityRevSus,String userId){
		if (BeanUtils.isNotEmpty(wQbdQualityRevSus)) {
			Iterator<WQbdQualityRevSu> iterator = wQbdQualityRevSus.iterator();
			while (iterator.hasNext()) {
				WQbdQualityRevSu next = iterator.next();
				if (!next.getPersionuserId().equals(userId)) {
					iterator.remove();
				}
			}
		}

	}



}
